﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Definitions;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Profiles
{
    public class Users : UserControl
    {
        protected Repeater LastRegistered;

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    var repository = new UserProfileRepository(SPContext.Current.Web);
                    var users = repository.GetLastJoinedUsers(6);
                    LastRegistered.DataSource = users;
                    LastRegistered.DataBind();
                }
            }
            catch(Exception ex)
            {
                Logger.LogWarning("Users.OnLoad(): exception");
                Logger.LogException(ex);
            }
        }

        private static DataTable GetLastRegisteredUsers(uint count)
        {
            try
            {
                var table = new DataTable();
                table.Columns.Add("ID", typeof(int));
                table.Columns.Add("Title", typeof(string));
                table.Columns.Add("ImageUrl", typeof(string));

                var siteId = SPContext.Current.Site.ID;
                var sec = new SPSecurity.CodeToRunElevated(delegate
                {
                    using (var site = new SPSite(siteId))
                    {
                        using (var web = site.OpenWeb("/"))
                        {
                            var query = new SPQuery();
                            query.Query = "<OrderBy><FieldRef Name='Created' Ascending='False' IncludeTimeValue='TRUE'/></OrderBy>";
                            //query.RowLimit = count;
                            var i = 0;
                            var items = web.GetList(Lists.ProfileList).GetItems(query);
                            foreach (SPListItem item in items)
                            {
                                if (string.IsNullOrEmpty(item.Title))
                                    continue;
                                if (i >= count)
                                    break;
                                i++;

                                var row = table.NewRow();
                                row["ID"] = item.ID;
                                row["Title"] = item.Title;

                                var imageUrl = "/_layouts/enetahandlers/userimagehandler.ashx?size=32&id=" + item.ID;
                                row["ImageUrl"] = imageUrl;
                                table.Rows.Add(row);
                            }
                        }
                    }
                });
                SPSecurity.RunWithElevatedPrivileges(sec);
                return table;
            }
            catch (Exception ex)
            {
                Logger.LogWarning("Users.GetLastRegisteredUsers(): exception");
                Logger.LogException(ex);
            }
            return null;
        }
    }
}
